package com.dddpeter.yater;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class Tab2Activity extends Activity {
	DrawView draw;
	final int MSG_SEND=0x100;
	final int MSG_CLEAR=0x102;
	final int MSG_DRAW_CIRCLE=0x103;
	private float x;
	private float y;
	final Handler handler=new Handler(){
		 public void handleMessage(android.os.Message msg){
			 NotificationManager notiManger=(NotificationManager)Tab2Activity.this.getSystemService(Tab2Activity.this.NOTIFICATION_SERVICE);
			 PendingIntent contentIntent=PendingIntent.getActivity(Tab2Activity.this, 0, 
			    		new Intent(Tab2Activity.this, Tab2Activity.class), 0);
			    Notification notify=new Notification();
		    switch(msg.what){		   
		    case MSG_CLEAR:
		    	DrawView.circleArray.clear();
		    	DrawView.draw=true;
		    	try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		    	finally{
		    		DrawView.draw=false;
		    	}    	
		    	break;
		    case MSG_DRAW_CIRCLE:
		    	Circle c=new Circle();
				c.setmX(Tab2Activity.this.x);
				c.setmY(Tab2Activity.this.y);
				c.setRadius((float) (Math.random()*70+30));
				int RED=(int) (Math.random()*255+1);
				int GREEN=(int) (Math.random()*255+1);
				int BLUE=(int) (Math.random()*255+1);
				Color color=new Color();	
				c.setColor(color.rgb(RED, GREEN, BLUE));		
				draw.circleArray.add(c);		
				draw.invalidate();
		    	break;
		    }
		    super.handleMessage(msg);
		 }
		 
	};
	public void drawCircle(View view){
		 Message msg=new Message();
			msg.what=MSG_DRAW_CIRCLE;
			handler.sendMessage(msg);
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab2);
		draw=(DrawView) findViewById(R.id.drawView1);
		 NotificationManager notiManger=(NotificationManager)Tab2Activity.this.getSystemService(Tab2Activity.this.NOTIFICATION_SERVICE);
		 notiManger.cancel(MSG_SEND);
		 OnTouchListener drawTouchListener =new   OnTouchListener(){
	    
			 

				public boolean onTouch(View arg0, MotionEvent event) {
					Tab2Activity.this.x=event.getX();
					Tab2Activity.this.y=event.getY();
					Message msg=new Message();
					msg.what=MSG_DRAW_CIRCLE;
		   			handler.sendMessage(msg);   			
					return true;
				}
		   			
		   	};
		draw.setOnTouchListener(drawTouchListener);

		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.tab2, menu);
		return true;
	}
	public void clearAll(View view){
		Message msg=new Message();
		msg.what=MSG_CLEAR;
		handler.sendMessage(msg);
	}


}
